package com.example.springboot_shiro.interceptor;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: lqy
 * @date: 2021/9/16 14:56
 * @description:
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        String msg = e.getMessage();
        if (msg == null || msg.equals("")) {
            msg = "服务器出错";
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", msg);
        return jsonObject;
    }

    @ResponseBody
    @ExceptionHandler(AuthorizationException.class)
    public Object authError(AuthorizationException e) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("message", "您没有访问权限");
        jsonObject.put("code", "401");
        return jsonObject;
    }


}
